﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using JLTPLAN;
using INTEX.DataBase;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class JLTPLAN : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        DataSet setDISPO;
        public string setID
        {
            get
            {
                return (string)txtSetDispo.Text.Trim();
            }
        }
        public SETINTEXDB setVKKOPF
        {
            get
            {
                return (SETINTEXDB)TryFindResource("setVKKOPF");
            }
        }
        public SETINTEXDB setVKPOS
        {
            get
            {
                return (SETINTEXDB)TryFindResource("setVKPOS");
            }
        }        
        JLTPLANData m_Data, m_DataX;

        private DataRow dwUI;
        JLTPLANReport m_rpt;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        string UserID = "";
        CollectionViewSource vwUIABPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwUIABPOS");
            }
        }
        CollectionViewSource vwWASTAMM
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwWASTAMM");
            }
        }
        string VKPAUFKRS
        {
            get
            {
                return m_Data.Left( txtVKPAUFKRS.Text.Trim()+"  ", 2);
            }
        }
        string VKPAUFLFD
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFLFD.Text.Trim(), 6);
            }
        }
        string VKPAUFPOS
        {
            get
            {
                return m_Data.Right("000" + txtVKPAUFPOS.Text.Trim(), 3);
            }
        }

        public JLTPLAN()
        {
            InitializeComponent();
            this.Name = "创建订单网络";
            this.Title = "创建订单网络";
        }
        #endregion
        #region Events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                m_Data = new JLTPLANData(OracleDB, proxy);
                txtUserID.Text = (string)dicParamater["User"];
                txtPassword.Password = (string)dicParamater["Pwd"];
                txtVKPAUFKRS.Text = "Z ";
                txtVKPAUFLFD.Text = "002280";
                txtVKPAUFPOS.Text = "004";
                ReloadCon();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count > 0)
            {
                DataRow dwUser = setResult.Tables[0].Rows[0];
                txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
                UserID = userID;
                m_Data.UserID = userID;
                this.UserID = userID;

                m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                    + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
                m_Data.MANDANT = Mandant;
                m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
                this.txtLogIn.Text = DateTime.Now.ToLocalTime().ToString() + ": " + userID + "已登录";
                txtLogIn.Background = new SolidColorBrush(Colors.Green);

            }
        }
        private void MergetListData()
        {
            try
            {

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        #region==========AV95ON======================

        private void btnGetVKPOS_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                GetVKPOS(VKPAUFKRS, VKPAUFLFD, VKPAUFPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void GetVKKOPF(string krs, string lfd)
        {
            setVKKOPF.Clear();
            DataSet setResult = new DataSet();
            setResult = m_Data.QueryVKKOPF(krs, lfd);
            setVKKOPF.Merge(setResult);
        }
        private void GetVKPOS(string krs, string lfd, string pos)
        {
            setVKPOS.Clear();
            DataSet setResult = new DataSet();
            setResult = m_Data.QueryVKPOS(krs, lfd, pos);
            setVKPOS.Merge(setResult);
            bool result=m_Data.Reset(setVKPOS, setDISPO);
            if (!result)
            {
                return;
            }
            PrePlan(krs, lfd, pos);
            if (vwWASTAMM.View != null)
                vwWASTAMM.View.MoveCurrentToFirst();
        }
        private void btnDATADISPO_Click(object sender, RoutedEventArgs e)
        {
            SETUPABON setupab = new SETUPABON();
            setupab.Show();
        }
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dgVKPOS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataSet setWAART = new DataSet();

            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    DataRow dwTemp = drv.Row;
                    string krs = dwTemp["VKPAUFKRS"].ToString();
                    string lfd = dwTemp["VKPAUFLFD"].ToString();
                    string pos = dwTemp["VKPAUFPOS"].ToString();
                    GetVKPOS(krs, lfd, pos);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnGetVKKOPF_Click(object sender, RoutedEventArgs e)
        {
            GetVKKOPF(VKPAUFKRS, VKPAUFLFD);
        }
        private void ReloadCon()
        {
            setDISPO = m_Data.LoadSetDispo(setID);
        }
        private void btnInsertABON_Click(object sender, RoutedEventArgs e)
        {
            using (new LongOperation(this, "正在创建"))
            {
                m_Data.Reset(setVKPOS, setDISPO);
                m_Data.Execute();
            }
        }
        public void PrePlan(string krs, string lfd, string pos)
        {
           setVKPOS.Merge(m_Data.PrePlan(krs, lfd, pos,setID));
           if (vwUIABPOS.View != null)
               vwUIABPOS.View.MoveCurrentToFirst();
        }
        private void btnRefreshABON_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrePlan(VKPAUFKRS, VKPAUFLFD, VKPAUFPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnDeleteABON_Click(object sender, RoutedEventArgs e)
        {
            m_Data.DeleteABON(VKPAUFKRS, VKPAUFLFD, VKPAUFPOS);

        }
    }
}
